$(document).ready(function(){});
function hidden() {
    $("#bs-box").hidden();
}

function slide() {
    $("#bs-box").slideToggle(0);
}

function show() {
    $("#bs-box").show();
}

function buy() {
    $("#order").submit();
}


function validate() {
   var username = $("#username").val(); 
   var passwd = $("#passwd").val(); 
   errname = $("#errorname");
   errpwd = $("#errorpwd");
   if (username == "") {
        errname.removeClass("hidden");
        $("#namediv").addClass("info")
        errname.html("请输入用户名");
        errname.addClass("help-inline");
   } 
        //errname.removeClass("hidden");
        //$("#namediv").addClass("warning")
        //errname.html("用户名不正确");
        //errname.addClass("help-inline");
   
   if (passwd == "") {
        errpwd.removeClass("hidden");
        $("#pwddiv").addClass("info")
        errpwd.html("请输入密码");
        errpwd.addClass("help-inline");
   } 
        //errpwd.removeClass("hidden");
        //$("#namediv").addClass("warning")
        //errpwd.html("密码不正确");
        //errpwd.addClass("help-inline");
    console.log('name: '+ username + ', passwd:' + passwd) 
    if (username != '' && passwd != '') {
        $.post(
            '/login', 
            {"username": username, "passwd": passwd},
            function(data) {
                console.log("data:" + data);
                if (data == 'False'){
        errname.removeClass("hidden");
        $("#namediv").addClass("warning")
        errname.html("用户名或密码不正确");
        errname.addClass("help-inline");
                } else {
                   window.location = '/';
                }
            }
        );
    }
}


function clearitem(input, div) {
    errname = $(input);
    namediv = $(div);
    if (errname.hasClass("help-inline")) {
        errname.removeClass("help-inline")
        errname.addClass("hidden");
    }

    if (namediv.hasClass("warning"))
           namediv.removeClass("warning");
   
    if (namediv.hasClass("info"))
           namediv.removeClass("info");
}

function closehint(div) {
    $(div).addClass('hidden');
}

function check(id, divid, htmltxt) {
        $(id).removeClass("hidden");
        $(divid).addClass("warning")
        $(id).html(htmltxt);
        $(id).addClass("help-inline");
}

function validate_signup() {
   var username = $("#username").val(); 
   var passwd = $("#passwd").val(); 
   var checkpasswd = $("#checkpasswd").val(); 
   var name = $("#name").val(); 
   var tel = $("#tel").val(); 
   var addr = $("#addr").val(); 
   var birth = $("#birth").val(); 
   var postcode = $("#postcode").val(); 
   console.log('uname= ' + username + ', passwd=' + passwd + ', cpasswd=' + checkpasswd
           + ', tel=' + tel +  '.addr=' + addr + ', birth=' + birth)
   if (username == '') {
       check('#erroruname', '#unamediv', '请输入用户名');
   } else if (passwd == '') {
       check('#errorpass', '#passdiv', '请输入密码');
   } else if (checkpasswd == '') {
       check('#errorcpass', '#checkdiv', '请确认密码');
   } else if (passwd != checkpasswd) {
       check('#errorcpass', '#checkdiv', '两次密码不一致');
   } else if (name == '') {
       check('#errorname', '#namediv', '请输入用户名');
   } else if (tel == '') {
       check('#errortel', '#teldiv', '请输入电话');
   } else if (addr == '') {
       check('#erroraddr', '#addrdiv', '请输入地址');
   } else {
        $.post(
            '/signup', 
            {
                "username": username, 
                "passwd": passwd,
                "checkpasswd": checkpasswd,
                "name": name,
                "tel": tel,
                "addr": addr,
                "birth": birth,
                "postcode": postcode
            },
            function(data) {
                console.log("data:" + data);
                if (data == 'False'){
                    $("#hint").removeClass("hidden")
                } else {
                   window.location = '/';
                }
            }
        );
   }
}

